package lab03.test08;

import java.io.*;

public class Test08 {
    public static void main(String[] args) {

        //定义文件路径
        File file = new File("D:\\java实验\\lab03test08-3.txt");
        String path1 = "D:\\java实验\\lab03test08-1.txt";
        String path2 = "D:\\java实验\\lab03test08-2.txt";

        //定义文件1和文件2的的输入流
        FileInputStream fileInputStream1 = null;
        FileInputStream fileInputStream2 = null;

        //定义文件3的输出流
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream1 = new FileInputStream(path1);
            fileInputStream2 = new FileInputStream(path2);

            fileOutputStream = new FileOutputStream(file);

            int data1 = 0;
            int data2 = 0;
            while (true) {
                if (((data1 = fileInputStream1.read()) != -1) && ((data2 = fileInputStream2.read()) != -1)) {
                    fileOutputStream.write(data1);
                    fileOutputStream.write(data2);
                } else if (((data1 = fileInputStream1.read()) == -1) && ((data2 = fileInputStream2.read()) != -1)) {
                    fileOutputStream.write(data2);
                } else if (((data1 = fileInputStream1.read()) != -1) && ((data2 = fileInputStream2.read()) == -1)) {
                    fileOutputStream.write(fileInputStream1.read());
                } else
                    break;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
